package bar.dark.lee;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import bar.dark.lee.beforesms.BeforeSmsListActivity;
import bar.dark.lee.common.BDAd;
import bar.dark.lee.macro.MacroListActivity;
import bar.dark.lee.memo.MemoListActivity;

public abstract class SuperActivity extends Activity
{
	public Context superContext = this;
	
	public static Typeface typeface = null;
	
	private View titleLayout;
	private TextView titleView;
	private TextView leftButton;
	private TextView rightButton;
	private ViewGroup bodyLayout;
	
	public ViewGroup superAdView;
	
	private BDAd ad = null;
	
	public String shape;
	
	public boolean isAdFlag = true;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		shape = pref.getString(BDConstents.CURRENCY_SHAPE, "원");
		shape = " " + shape;
		
		super.setContentView(R.layout.super_activity);
		
		titleLayout = findViewById(R.id.titleLayout);
		titleView = (TextView) findViewById(R.id.superTitleView);
		bodyLayout = (ViewGroup) findViewById(R.id.bodyLayout);
		leftButton = (TextView) findViewById(R.id.superLeftButton);
		rightButton = (TextView) findViewById(R.id.superRightButton);
		
		superAdView = (ViewGroup) findViewById(R.id.superAdview);
		
		leftButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				onBackPressed();
			}
		});
		
		rightButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				tipButton();
			}
		});
		
		if (isAdFlag)
		{
			startAd();
		}
	}
	
	protected void startAd()
	{
		ad = new BDAd(this, (LinearLayout)superAdView);
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		
		if (ad != null)
		{
			ad.resumeAd();
		}
	}
	
	@Override
	protected void onPause()
	{
		super.onPause();
		
		if (ad != null)
		{
			ad.pauseAd();
		}
	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		
		if (ad != null)
		{
			ad.destoryAd();
		}
	}
	
	@Override
	protected void onNewIntent(Intent intent)
	{
		super.onNewIntent(intent);
		
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		shape = pref.getString(BDConstents.CURRENCY_SHAPE, "원");
		shape = " " + shape;
		
		int fontFlag = pref.getInt(BDConstents.FONT_SETTING, 1);
		
		if (fontFlag == 0)
		{
			typeface = null;
		}
		else if (fontFlag == 1)
		{
			if (typeface == null)
				typeface = Typeface.createFromAsset(getAssets(), BDConstents.FONT_FILE_NAME);
		}
		
		ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
		setGlobalFont(root);  
	}
	
	@Override
	public void setContentView(int layoutResID)
	{
		LayoutInflater inflater = getLayoutInflater();
		View view = inflater.inflate(layoutResID, null);
		this.setContentView(view);
	}
	
	@Override
	public void setContentView(View view) {
		this.bodyLayout.removeAllViews();
		
		if(view==null)
		{
			return;
		}
		
		ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
		
		if(layoutParams == null)
		{
			layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		}
		else
		{
			layoutParams.width = LayoutParams.FILL_PARENT;
			layoutParams.height = LayoutParams.FILL_PARENT;
		}
		
		view.setLayoutParams(layoutParams);
		
		this.bodyLayout.addView(view);
		
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int fontFlag = pref.getInt(BDConstents.FONT_SETTING, 1);
		
		if (fontFlag == 1)
		{
			if (typeface == null)
				typeface = Typeface.createFromAsset(getAssets(), BDConstents.FONT_FILE_NAME);
			
			ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
			setGlobalFont(root);  
		}
	}
	
	private void setGlobalFont(ViewGroup root) 
	{
	    for (int i = 0; i < root.getChildCount(); i++) 
	    {
	        View child = root.getChildAt(i);
	        
	        if (child instanceof TextView)
	        {
	        	((TextView)child).setTypeface(typeface);
	        }
	        else if (child instanceof EditText)
			{
	        	((EditText)child).setTypeface(typeface);
			}
	        else if (child instanceof Button)
			{
	        	((Button)child).setTypeface(typeface);
			}
	        else if (child instanceof ViewGroup)
	        {
	        	setGlobalFont((ViewGroup)child);
	        }
	    }
	}
	
	@Override
	public void setTitle(CharSequence title)
	{
		titleView.setText(title);
	}

	@Override
	public void setTitle(int titleId)
	{
		titleView.setBackgroundResource(titleId);
	}
	
	/**
	 * 오른쪽 팁 버튼을 안보이게 한다.
	 */
	public void setGoneTipButton()
	{
		rightButton.setVisibility(View.INVISIBLE);
	}
	
	/**
	 * 타이틀을 안보이게 한다.
	 */
	public void setGoneTitle()
	{
		titleLayout.setVisibility(View.GONE);
	}
	
	/**
	 * 도움말 버튼의 문구를 변경한다.
	 * @param text
	 */
	public void setTextTip(String text)
	{
		rightButton.setText(text);
	}
	
	/**
	 * 도움말 버튼입니다.
	 */
	protected abstract void tipButton();
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		menu.add(0, 0, 0, "계산기");
		menu.add(0, 1, 1, "메모");
		menu.add(0, 4, 4, "빠른입력");
		menu.add(0, 5, 5, "문자 한번에 입력");
		
		if (BDConstents.TEST)
		{
			menu.add(0, 9, 9, "문자테스트");
		}
		
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		switch (item.getItemId()) 
		{
			case 0:
				showCalculator();
				break;
			case 1:
				showMemo();
				break;
			case 4:
				showMacro();
				break;
			case 5:
				showBeforeSms();
			case 9:
//				smsTest();
				break;
			default:
				break;
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	private void showCalculator()
	{
		Intent intent = new Intent(superContext, CalculatorActivity.class);
		startActivity(intent);
	}
	
	private void showMemo()
	{
		Intent intent = new Intent(superContext, MemoListActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivity(intent);
	}
	
	private void showMacro()
	{
		Intent intent = new Intent(superContext, MacroListActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivity(intent);
	}
	
	private void showBeforeSms()
	{
		Intent intent = new Intent(superContext, BeforeSmsListActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivity(intent);
	}
	
//	private void smsTest()
//	{
//		new SmsTest(superContext);
//	}
}
